package cn.wgx.module.mind.service;

import cn.wgx.common.entity.mind.MindGroup;
import cn.wgx.module.mind.entity.mapper.GroupMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

@Service
public class GroupService extends BaseMindService<GroupMapper, MindGroup> {

    @Autowired
    private BeatService beatService;

    @Transactional(readOnly = true)
    public List<MindGroup> getListByProId(Integer pro_id){
        MindGroup mindBeat = new MindGroup();
        mindBeat.setPro_id(pro_id);
        List<MindGroup> mindBeats = baseMapper.selectList(new QueryWrapper<>(mindBeat).orderBy(true, true, "sort"));
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("pro_id", 1);
        queryWrapper.orderBy(true, true, "x");
        baseMapper.selectList(queryWrapper);
        return mindBeats;
    }


    @Transactional()
    public boolean removeById(Serializable id){
        boolean b = super.removeById(id);
        destroy(id);
        return b;
    }

    @Transactional()
    public boolean removeByIds(Collection<? extends Serializable> ids){
        boolean b = super.removeByIds(ids);
        destroy(ids);
        return b;
    }


    private void destroy(Collection<? extends Serializable> mindBeats){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("group_id", mindBeats);
        beatService.remove(queryWrapper);
    }


    private void destroy(Serializable id){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("group_id", id);
        beatService.remove(queryWrapper);
    }
}
